package com.dc.trans.steps.transactionstart;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStepMeta;
import org.pentaho.di.trans.step.StepDialogInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.ui.trans.step.BaseStepDialog;

/**
* @ClassName: TransactionStartDialog
* @Description: TODO
* @author zhangbinp
* @date Jun 21, 2011 10:45:58 AM
*/
public class TransactionStartDialog extends BaseStepDialog implements StepDialogInterface {

	private static Class<?> PKG = TransactionStartMeta.class;
	private TransactionStartMeta	input;
	private Map<String, Integer> inputFields;

	private Label wlConnection;
	private CCombo wConnection;
	private Button wbnConnection, wbeConnection;	
	private boolean transactionEndExist = false;

	public TransactionStartDialog(Shell parent, Object in, TransMeta tr, String sname) {
		super(parent, (BaseStepMeta) in, tr, sname);
		input = (TransactionStartMeta) in;
		inputFields =new HashMap<String, Integer>();
	}

	@Override
	public String open() {
		// TODO Auto-generated method stub
		Shell parent = getParent();
		Display display = parent.getDisplay();
		
		shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.MIN);
 		props.setLook(shell);
        setShellImage(shell, input);
        
        ModifyListener lsMod = new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				input.setChanged();
			}
		};
		ModifyListener lsTableMod = new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				input.setChanged();
			}
		};
		SelectionListener lsSelection = new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e) 
			{
				input.setChanged();
			}
		};
		changed = input.hasChanged();

		FormLayout formLayout = new FormLayout();
		formLayout.marginWidth = Const.FORM_MARGIN;
		formLayout.marginHeight = Const.FORM_MARGIN;

		shell.setLayout(formLayout);
		shell.setText(BaseMessages.getString(PKG,"TransactionStartDialog.Shell.Title")); //$NON-NLS-1$

		int middle = props.getMiddlePct();
		int margin = Const.MARGIN;
		
		// Stepname line
		wlStepname = new Label(shell, SWT.RIGHT);
		wlStepname.setText(BaseMessages.getString(PKG,"System.Label.StepName")); //$NON-NLS-1$
		props.setLook(wlStepname);
		fdlStepname = new FormData();
		fdlStepname.left = new FormAttachment(0, 0);
		fdlStepname.right = new FormAttachment(middle, -margin);
		fdlStepname.top = new FormAttachment(0, margin);
		wlStepname.setLayoutData(fdlStepname);
		wStepname = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
		wStepname.setText(stepname);
		props.setLook(wStepname);
		wStepname.addModifyListener(lsMod);
		fdStepname = new FormData();
		fdStepname.left = new FormAttachment(middle, 0);
		fdStepname.top = new FormAttachment(0, margin);
		fdStepname.right = new FormAttachment(100, 0);
		wStepname.setLayoutData(fdStepname);
	
		// Connection line
		wConnection = addConnectionLine(shell, wStepname, middle, margin);
		if (input.getDatabaseMeta()==null && transMeta.nrDatabases()==1) wConnection.select(0);
		wConnection.addModifyListener(lsMod);
		wConnection.addSelectionListener(lsSelection);
		
		// THE BUTTONS
		wOK = new Button(shell, SWT.PUSH);
		wOK.setText(BaseMessages.getString(PKG, "System.Button.OK")); //$NON-NLS-1$
		wCancel = new Button(shell, SWT.PUSH);
		wCancel.setText(BaseMessages.getString(PKG, "System.Button.Cancel")); //$NON-NLS-1$

		setButtonPositions(new Button[] { wOK, wCancel}, margin, wConnection);
		
		
		// Add listeners
		lsCancel = new Listener() {
			public void handleEvent(Event e) {
				cancel();
			}
		};
		lsOK = new Listener() {
			public void handleEvent(Event e) {
				ok();
			}
		};
		wCancel.addListener(SWT.Selection, lsCancel);
		wOK.addListener(SWT.Selection, lsOK);
		setSize();
		
		getData();
		input.setChanged(changed);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return stepname;
		
		
		
		
		
		
	}
	/**
	 * Copy information from the meta-data input to the dialog fields.
	 */ 
	public void getData(){
		wStepname.selectAll();
		if (input.getDatabaseMeta() != null)
			wConnection.setText(input.getDatabaseMeta().getName());
		
	}
	private void ok() {
		if (Const.isEmpty(wStepname.getText()))
			return;
		getInfo(input);
		checkTailExits();
		if(!transactionEndExist){
			MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION );
			mb.setMessage(BaseMessages.getString(PKG, "TransactionStartDialog.tip.DialogMessage")); //$NON-NLS-1$
			mb.setText(BaseMessages.getString(PKG, "TransactionStartDialog.tip.DialogTitle")); //$NON-NLS-1$
			mb.open();
		}
		dispose();
	}
	private void getInfo(TransactionStartMeta meta) {
		meta.setDatabaseMeta(  transMeta.findDatabase(wConnection.getText()) );
		meta.setStepName(wStepname.getText());
		stepname = wStepname.getText(); // return value
	}
	
	private void cancel() {
		stepname = null;
		input.setChanged(changed);
		dispose();
	}
	public void checkTailExits(){
		StepMeta stepinfo = transMeta.findStep(stepname);
		try {
			findTransactionEndStepMeta(stepinfo);
		} catch (KettleXMLException e1) {
			e1.printStackTrace();
		} catch (KettleException e1) {
			e1.printStackTrace();
		}
	}
	//find a previous transactionStepMeta by StepMeta Object
	public void findTransactionEndStepMeta(StepMeta stepinfo) throws KettleXMLException, KettleException{
		if (stepinfo!=null)
		{
			List<StepMeta> nextSteps = transMeta.findNextSteps(stepinfo);
			for (int i=0;i<nextSteps.size();i++)
			{
				StepMeta stepMeta = nextSteps.get(i);
				if("TransactionEnd".equals(stepMeta.getStepID())){
					transactionEndExist = true;
					return;
				}
				findTransactionEndStepMeta(stepMeta);
			}
		}
	}
}
